home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / misc / gethelp / source / main.c < prev    next >
C/C++ Source or Header  |  1999-06-14  |  6KB  |  177 lines

  1. #include "gui.h"
  2.  
  3. #define LINES       1024        /* max length of html file */
  4. #define MAXLEN      256         /* max length of a string */
  5. #define MAXFILES    256         /* max number of files */
  6.  
  7. #define RESULTHTML  "_results.html"
  8.  
  9. /**********************/
  10. /* global definitions */
  11. /**********************/
  12. int debug = 0;              /* global debug flag */
  13.  
  14. char *filesptr[MAXFILES];   /* list of filenames */
  15.  
  16. struct Remember *RememberFilesPtr = NULL,
  17.                 *RememberLinesPtr = NULL;   /* memory pointers */
  18.  
  19. /************************/
  20. /* function prototypes  */
  21. /************************/
  22.  
  23. /* report.c */
  24. void start_report(), add_url(), finish_report();
  25.  
  26. /* dirlist.c */
  27. int dirlist();
  28.  
  29. /* get_title.c */
  30. int findstring(), findtitle(), readlines();
  31.  
  32. /************************/
  33. /* main function        */
  34. /************************/
  35.  
  36. int main(int argc,char *argv[])
  37. {
  38.  
  39.     int gui_command = GO_RUNNING;   /* gui control flag */
  40.     ULONG sigs = 0;                 /* input signals */
  41.     char *search_text = "";         /* search string from MUI App */
  42.     int flag_whole,                 /* whole word flag */
  43.         flag_case;                  /* match case flag */
  44.  
  45.     struct ObjApp *App;             /* MUI Application */
  46.  
  47.     FILE *html_fp, *report_fp, *fopen();
  48.  
  49.     char *lineptr[LINES],   /* storage for html file */
  50.          target[MAXLEN];    /* target string */
  51.  
  52.     int i,              /* loop counter */
  53.         nlines;         /* number of lines in a file */
  54.     int nfiles = 0;     /* number of files found */
  55.     int nfound = 0;     /* number of matching files found */
  56.  
  57.     /* check number of arguments */
  58.     switch (argc)
  59.         {
  60.             case 1: /* string supplied by gui */
  61.  
  62.                 /* Open the MUI interface */
  63.                 App = creategui();
  64.  
  65.                 while (!gui_command)
  66.                     {
  67.                         /* Check for user inputs */
  68.                         switch (DoMethod(App->app,MUIM_Application_NewInput,&sigs))
  69.                             {
  70.                                 case MUIV_Application_ReturnID_Quit:
  71.                                     gui_command = GO_QUIT;
  72.                                     break;
  73.  
  74.                                 case GO_SEARCH:
  75.                                     get(App->string1,MUIA_String_Contents,&search_text);
  76.                                     gui_command = GO_SEARCH;
  77.                                     break;
  78.  
  79.                             }
  80.                         if (!gui_command && sigs) Wait(sigs | SIGBREAKF_CTRL_C);
  81.                         if (sigs & SIGBREAKF_CTRL_C) break;
  82.  
  83.                     }
  84.  
  85.                 /* check what happened */
  86.                 if (gui_command == GO_SEARCH)
  87.                     {
  88.                         strcpy(target,search_text);
  89.                         /*printf("search string = %s\n",target);*/
  90.  
  91.                         get(App->checkmark1,MUIA_Selected,&flag_whole);
  92.  
  93.                         /*if (flag_whole == TRUE) printf ("Match whole word!\n");
  94.                             else printf ("Match partial word!\n");*/
  95.  
  96.                         get(App->checkmark2,MUIA_Selected,&flag_case);
  97.  
  98.                         /*if (flag_case == TRUE) printf ("case sensitive search!\n");
  99.                             else printf ("accept upper/lower case!\n");*/
  100.                     }
  101.  
  102.                 /* Dispose of the interface */
  103.                 DisposeApp(App);
  104.                 break;
  105.  
  106.             case 2: /* string already supplied */
  107.                 strcpy(target, argv[1]);
  108.                 break;
  109.  
  110.             default:
  111.                 printf("Usage: get_title [STRING/S]\n");
  112.                 exit(1);
  113.         }
  114.  
  115.     /* quit if required */
  116.     if (gui_command == GO_QUIT) exit(1);
  117.  
  118.     /* read in current directory list */
  119.     nfiles = dirlist();
  120.  
  121.     /* report how many files found */
  122.     if ((report_fp = fopen(RESULTHTML,"w")) == NULL) {
  123.         printf("Can't open report file for writing!\n");
  124.         exit(1);
  125.     } else start_report(report_fp, target, flag_case, flag_whole, nfiles);
  126.  
  127.     /* open each file in dirlist() */
  128.     for (i = 0; i < nfiles; i++) {
  129.  
  130.         /* attempt to open file */
  131.         if ((html_fp = fopen(filesptr[i], "r")) == NULL) {
  132.             printf("get_title: can't open %s\n", filesptr[i]);
  133.             exit(1);
  134.         } 
  135.  
  136.         /* read file into lineptr */
  137.         if ((nlines = readlines(lineptr, LINES, html_fp)) <0) {
  138.              printf("file too long to process!\n");
  139.              exit(1);
  140.         }
  141.         
  142.         /* close off the html file */
  143.         fclose(html_fp);
  144.  
  145.         /* search for target */
  146.         if (findstring(lineptr, nlines, target, flag_whole, flag_case)) {
  147.  
  148.              nfound++;
  149.  
  150.              /* find title of matching file */
  151.              if (!findtitle(filesptr[i], lineptr, nlines, report_fp))
  152.                  printf("Error: No <TITLE> in document!\n");
  153.         }
  154.  
  155.         /* release memory */
  156.         if (RememberLinesPtr) {
  157.  
  158.             FreeRemember(&RememberLinesPtr, TRUE);
  159.             if (debug) if (!RememberLinesPtr) printf("RememberLinesPtr memory released..\n");
  160.                             else printf("RememberLinesPtr is still set to 0x%x\n",&RememberLinesPtr);
  161.         } else printf("Error! RememberLinesPtr was NULL\n");
  162.     }
  163.  
  164.     /* polish up report file */
  165.     finish_report(report_fp, nfound);
  166.     fclose(report_fp);
  167.  
  168.     /* cleanup memory */
  169.     if (RememberFilesPtr) {
  170.         FreeRemember(&RememberFilesPtr, TRUE);
  171.         if (debug) if (!RememberFilesPtr) printf("RememberFilesPtr memory released..\n");
  172.                             else printf("RememberFilesPtr is still set to 0x%x\n",&RememberFilesPtr);
  173.     } else printf("Error! RememberFilesPtr was NULL\n");
  174.  
  175. }
  176.  
  177.